package libs

import (
	"encoding/json"
	"gin_study/StringBuffer"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Query(context *(gin.Context)) {
	username, flag := context.GetQuery("user")
	if flag {
		context.String(200, "用户是"+username)
	} else {
		context.String(200, "没拿到 哼唧唧")
	}
}

func Querys(context *(gin.Context)) {
	usernames := context.QueryArray("user")
	context.JSON(200, usernames)
}

func GetPage(context *(gin.Context)) {
	value := context.Param("number")
	context.String(http.StatusOK, "你在查询第"+value+"页的数据")
}

func KeywordGetPage(context *(gin.Context)) {
	page := context.Param("keyword")
	keyword := context.Param("number")
	context.String(
		http.StatusOK,
		StringBuffer.FormatSuperFast("你在查询第%s页的%s数据", page, keyword),
	)
}

func FormRequest(context *(gin.Context)) {
	username := context.PostForm("UserName")
	context.String(200, "欢迎回来"+username)
}

type UserLoginVli struct {
	Username interface{}
	Password interface{}
}

func Primitive(context *(gin.Context)) {
	body, _ := context.GetRawData()
	reqType := context.GetHeader("Content-Type") // 获取请求头
	switch reqType {
	case "application/json":
		var userLoginVli UserLoginVli
		err := json.Unmarshal(body, &userLoginVli)
		if err != nil {
			context.String(201, "错误了！"+string(err.Error())+"呼噶~")
		} else {
			context.String(200, "Ciallo~ "+userLoginVli.Username.(string))
		}

	}
}
